<?php

use app\modules\Admin\models\memberpickgoodscardModel;
use app\modules\Admin\models\memberpickgoodslogModel;
use app\modules\Admin\models\pickgoodscardModel;
use Illuminate\Database\Capsule\Manager as DB;
#[用户提货卡使用日志管理]
class MemberpickgoodscardusedlogController extends AcommonController
{
    public function init() {
        $this->model = new memberpickgoodslogModel();
        $this->table = 'member_pickgoods_log';
        $this->primaryKey = 'id';
        parent::init();
    }

    #[用户提货卡使用日志列表]
    public function indexAction($returnData = FALSE)
    {
        $this->model = $this->model->with(['member_pickgoods_card', 'pickgoods_card', 'member'])->whereIn('event', ['pickgoods', 'transfer', 'refund']);
        if($this->isPost()){
            $post_data = $this->postData;
            $search_data = [];
            if(isset($post_data['mode']) && !empty($post_data['mode']) && $post_data['mode'] != 'all'){
                array_push($search_data, ['event', '=', $post_data['mode']]);
            }
            if(isset($post_data['cardused_start']) && !empty($post_data['cardused_start'])){
                $this->model = $this->model->whereDate('created_at', '>=', $post_data['cardused_start']);
            }
            if(isset($post_data['cardused_end']) && !empty($post_data['cardused_end'])){
                $this->model = $this->model->whereDate('created_at', '<=', $post_data['cardused_end']);
            }
            $keywordsTypeArray = [
                'user_card_id' => 'user_card_id',
                'card_sn' => 'card_sn',
                'user_id' => 'user_id',
                'user_phone' => 'user_phone',
                'card_id' => 'card_id',
                'card_name' => 'card_name',
            ];
            if(isset($post_data['keywordsType']) && isset($post_data['keywords'])
                && in_array($post_data['keywordsType'], $keywordsTypeArray)
                && !empty($post_data['keywords'])){
                if($post_data['keywordsType'] == $keywordsTypeArray['user_card_id'] && is_numeric($post_data['keywords']) && intval($post_data['keywords']) > 0){
                    array_push($search_data, ['member_pickgoods_card_id', '=', $post_data['keywords']]);
                }
                if($post_data['keywordsType'] == $keywordsTypeArray['user_id'] && is_numeric($post_data['keywords']) && intval($post_data['keywords']) > 0){
                    array_push($this->condition_in, ['member_pickgoods_card_id', memberpickgoodscardModel::query()->where('member_id', $post_data['keywords'])->pluck('id')->toArray()]);
                }
                if($post_data['keywordsType'] == $keywordsTypeArray['user_phone']){
                    if(mb_strlen($post_data['keywords'], 'utf-8') < 4){
                        array_push($search_data, ['id', '=', 0]);
                    }else{
                        $member_ids = memberModel::query()->where('phone', 'like', "%{$post_data['keywords']}%")->pluck('id')->toArray();
                        array_push($this->condition_in, ['member_pickgoods_card_id', memberpickgoodscardModel::query()->whereIn('member_id', $member_ids)->pluck('id')->toArray()]);
                    }
                }
                if($post_data['keywordsType'] == $keywordsTypeArray['card_sn']){
                    if(mb_strlen($post_data['keywords'], 'utf-8') < 4){
                        array_push($search_data, ['id', '=', 0]);
                    }else{
                        array_push($this->condition_in, ['member_pickgoods_card_id', memberpickgoodscardModel::query()->where('card_sn', 'like', "%{$post_data['keywords']}%")->pluck('id')->toArray()]);
                    }
                }
                if($post_data['keywordsType'] == $keywordsTypeArray['card_id'] && is_numeric($post_data['keywords']) && intval($post_data['keywords']) > 0){
                    array_push($this->condition_in, ['member_pickgoods_card_id', memberpickgoodscardModel::query()->where('card_id', $post_data['keywords'])->pluck('id')->toArray()]);
                }
                if($post_data['keywordsType'] == $keywordsTypeArray['card_name']){
                    if(mb_strlen($post_data['keywords'], 'utf-8') < 2){
                        array_push($search_data, ['id', '=', 0]);
                    }else{
                        $card_ids = pickgoodscardModel::query()->where('name', 'like', "%{$post_data['keywords']}%")->pluck('id')->toArray();
                        array_push($this->condition_in, ['member_pickgoods_card_id', memberpickgoodscardModel::query()->whereIn('card_id', $card_ids)->pluck('id')->toArray()]);
                    }
                }
            }
            unset($keywordsTypeArray);
            $this->search = $search_data;
        }
        return parent::indexAction($returnData); // TODO: Change the autogenerated stub
    }
}